package com.gengzp.ops.controller;

import com.gengzp.common.constants.PermissionConstants;
import com.gengzp.common.document.annotations.Api;
import com.gengzp.common.document.annotations.ApiOperation;
import com.gengzp.common.model.base.req.SearchByIdReq;
import com.gengzp.common.model.base.resp.Result;
import com.gengzp.common.model.nacos.req.*;
import com.gengzp.common.model.nacos.resp.PageServiceInstanceResp;
import com.gengzp.common.model.nacos.resp.PageYamlResp;
import com.gengzp.common.model.nacos.resp.SearchNacosConfigResp;
import com.gengzp.common.model.nacos.resp.SearchYamlResp;
import com.gengzp.common.model.pagination.req.PaginationReq;
import com.gengzp.common.model.pagination.resp.PaginationResult;
import com.gengzp.ops.service.INacosService;
import com.gengzp.permission.annotations.CheckPermission;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @ClassName NacosController
 * @Description nacos管理模块控制层
 * @Author gengzp
 * @Date 2025/5/29 20:54
 */
@RestController
@RequestMapping("/nacos")
@Api(tags = "nacos服务治理模块")
public class NacosController {

    @Autowired
    private INacosService nacosService;

    /**
     * 添加nacos配置信息
     */
    @PostMapping("/add_config")
    @ApiOperation(value = "添加nacos配置信息")
    @CheckPermission(PermissionConstants.NACOS_MANAGE)
    public Result<Void> addNacosConfig(@Valid @RequestBody AddNacosConfigReq req) {
        nacosService.addNacosConfig(req);
        return Result.getSuccessResult();
    }

    /**
     * 编辑nacos配置信息
     */
    @PostMapping("/edit_config")
    @ApiOperation(value = "编辑nacos配置信息")
    @CheckPermission(PermissionConstants.NACOS_MANAGE)
    public Result<Void> editNacosConfig(@Valid @RequestBody EditNacosConfigReq req) {
        nacosService.editNacosConfig(req);
        return Result.getSuccessResult();
    }

    /**
     * 查询nacos配置信息列表
     */
    @PostMapping("/search_list")
    @ApiOperation(value = "查询nacos配置信息列表")
    public Result<List<SearchNacosConfigResp>> searchNacosConfigList() {
        return Result.getSuccessResult(nacosService.searchNacosConfigList());
    }

    /**
     * 根据id查询nacos配置信息详情
     */
    @PostMapping("/search_by_id")
    @ApiOperation(value = "根据id查询nacos配置信息详情")
    @CheckPermission(PermissionConstants.NACOS_MANAGE)
    public Result<SearchNacosConfigResp> searchNacosConfigById(@Valid @RequestBody SearchByIdReq req) {
        return Result.getSuccessResult(nacosService.searchNacosConfigById(Long.valueOf(req.getId())));
    }

    /**
     * 根据id删除nacos配置信息
     */
    @PostMapping("/delete_by_id")
    @ApiOperation(value = "根据id删除nacos配置信息")
    @CheckPermission(PermissionConstants.NACOS_MANAGE)
    public Result<Void> deleteNacosConfigById(@Valid @RequestBody SearchByIdReq req) {
        nacosService.deleteNacosConfigById(Long.valueOf(req.getId()));
        return Result.getSuccessResult();
    }

    /**
     * 分页查询yaml配置信息
     */
    @PostMapping("page_yaml")
    @ApiOperation(value = "分页查询yaml配置信息")
    public Result<PaginationResult<PageYamlResp>> pageQueryYamlConfig(@Valid @RequestBody PageYamlReq req) {
        return Result.getSuccessResult(nacosService.pageQueryYamlConfig(req));
    }

    /**
     * 同步yaml配置到数据库
     */
    @PostMapping("/sync_yaml_to_database")
    @ApiOperation(value = "同步yaml配置到数据库")
    @CheckPermission(PermissionConstants.NACOS_MANAGE)
    public Result<Void> syncYamlToDatabase() {
        nacosService.syncYamlToDatabase();
        return Result.getSuccessResult();
    }

    /**
     * 根据dataId查询yaml配置信息
     */
    @PostMapping("/search_yaml_by_data_id")
    @ApiOperation(value = "根据dataId查询yaml配置信息")
    @CheckPermission(PermissionConstants.NACOS_MANAGE)
    public Result<SearchYamlResp> searchYamlConfigByDataId(@Valid @RequestBody SearchYamlByDataIdReq req) {
        return Result.getSuccessResult(nacosService.searchYamlConfigByDataId(req.getDataId()));
    }

    /**
     * 编辑yaml配置信息
     */
    @PostMapping("/edit_yaml")
    @ApiOperation(value = "编辑yaml配置信息")
    @CheckPermission(PermissionConstants.NACOS_MANAGE)
    public Result<Void> editYamlConfig(@Valid @RequestBody EditYamlReq req) {
        nacosService.editYamlConfig(req);
        return Result.getSuccessResult();
    }

    /**
     * 分页查询在线服务实例信息
     */
    @PostMapping("/page_service_instance")
    @ApiOperation(value = "分页查询在线服务实例信息")
    public Result<PaginationResult<PageServiceInstanceResp>> pageQueryServiceInstance(@Valid @RequestBody PaginationReq req) {
        return Result.getSuccessResult(nacosService.pageQueryServiceInstance(req));
    }

}
